package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.OneDetailsModel;
import com.banshuibao.bsb_android.model.OneModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.PriceUtil;
import com.banshuibao.bsb_android.util.TextPaintUtils;
import com.banshuibao.bsb_android.util.TimeUtil;

public class CommonActivity extends BaseActivity implements View.OnClickListener {

    private TextView question, mineName, mineDate, mineMoney, fristAskName, fristAskContent, fristAskDate;
    private ImageView image_one;
    private ImageView image_two;
    private ImageView image_three;
    private RelativeLayout relativeLayout, answer, relativeLayout3, relativeLayout4, relativeLayout5, relativeLayout6;
    private ImageView head;
    private TextView name, noAnswer, askBtn1, askBtn2;
    private TextView content, secondAskName, secondAskContent, secondAskDate;
    private TextView common_date, fristName, fristContent, fristDate, secondName, secondContent, secondDate;
    private String[] split;
    private LinearLayout linearLayout;
    private ImageView mineImg, fristImg, secondImg;
    //本地名字和手机号
    private String name1;
    private String phoneNumber;
    private double price;
    private String uuid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common);
        initView();
        OneModel.DataBean item = (OneModel.DataBean) getIntent().getSerializableExtra("item");
        uuid = item.getUuid();
        price = item.getPrice();
        if (!TextUtils.isEmpty(uuid)) {
            setPost(uuid);
        }
    }

    private void setPost(String uuid) {
        OneDetailsModel.body body = new OneDetailsModel.body();
        body.setUuid(uuid);
        body.setIsWho("me");
        HttpUtil.doPost(Constants.Url.ONE_LIST_DETAILS, body, new HttpResponse(this, OneDetailsModel.class) {
            @Override
            public void onResult(Object obj) {
                OneDetailsModel s = (OneDetailsModel) obj;
                OneDetailsModel.DataBean data = s.getData();
                getData(data);
            }
        });
    }

    private void getData(OneDetailsModel.DataBean item) {
        GetLoginInfo loginInfo = GetLoginInfo.getLoginInfoInstance(this).readUser();
        name1 = loginInfo.getName();
        phoneNumber = loginInfo.getPhoneNumber();
        String headImg1 = loginInfo.getHeadImg();
        ImageUtil.loadImageHead(this, headImg1, mineImg);
        if (!TextUtils.isEmpty(name1)) {
            mineName.setText(name1);
            TextPaintUtils.textMothed(mineName);
        } else {
            mineName.setText(phoneNumber);
            TextPaintUtils.textMothed(mineName);
        }
        mineMoney.setText("¥"+ PriceUtil.FormatDecimal(item.getPrice()));
        if (item != null) {
            String question1 = item.getQuestion();
            String imgs = item.getImgs();
            question.setText(question1);
            String time = null;
            if (item.getCreateDate() != 0) {
                time = TimeUtil.format(item.getCreateDate());
            } else {
                time = "";
            }
            mineDate.setText(time);
            if (!TextUtils.isEmpty(imgs)) {
                split = imgs.split(",");
                if (split.length > 0) {
                    if ("null".equals(split[0])) {
                        return;
                    }
                    if (split.length == 1) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        image_two.setVisibility(View.GONE);
                        image_three.setVisibility(View.GONE);
                    } else if (split.length == 2) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        ImageUtil.loadImage(this, split[1], image_two);
                        image_three.setVisibility(View.GONE);
                    } else if (split.length == 3) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        ImageUtil.loadImage(this, split[1], image_two);
                        ImageUtil.loadImage(this, split[2], image_three);
                    }
                }
            } else {
                image_one.setVisibility(View.GONE);
                image_two.setVisibility(View.GONE);
                image_three.setVisibility(View.GONE);
                linearLayout.setVisibility(View.GONE);
            }

            String status1 = item.getStatus();
            switch (status1) {
                //未回答
                case "0":
                    noAnswer.setVisibility(View.VISIBLE);
                    break;
                //已回答
                case "1":
                    answer.setVisibility(View.VISIBLE);
                    relativeLayout.setVisibility(View.VISIBLE);
                    askBtn1.setVisibility(View.VISIBLE);
                    setDataOne(item);
                    break;
                //第一次追问
                case "2":
                    answer.setVisibility(View.VISIBLE);
                    relativeLayout.setVisibility(View.VISIBLE);
                    relativeLayout3.setVisibility(View.VISIBLE);
                    setDataTwo(item);
                    break;
                //专家第一次回答追问
                case "3":
                    answer.setVisibility(View.VISIBLE);
                    relativeLayout.setVisibility(View.VISIBLE);
                    relativeLayout3.setVisibility(View.VISIBLE);
                    relativeLayout4.setVisibility(View.VISIBLE);
                    askBtn2.setVisibility(View.VISIBLE);
                    setDataThree(item);
                    break;
                //第二次追问
                case "4":
                    answer.setVisibility(View.VISIBLE);
                    relativeLayout.setVisibility(View.VISIBLE);
                    relativeLayout3.setVisibility(View.VISIBLE);
                    relativeLayout4.setVisibility(View.VISIBLE);
                    relativeLayout5.setVisibility(View.VISIBLE);
                    setDataFour(item);
                    break;
                //专家回答第二次追问
                case "5":
                    answer.setVisibility(View.VISIBLE);
                    relativeLayout.setVisibility(View.VISIBLE);
                    relativeLayout3.setVisibility(View.VISIBLE);
                    relativeLayout4.setVisibility(View.VISIBLE);
                    relativeLayout5.setVisibility(View.VISIBLE);
                    relativeLayout6.setVisibility(View.VISIBLE);
                    setDataFive(item);
                    break;
                //过期
                case "6":

                    break;
            }
        }
    }

    private void setDataOne(OneDetailsModel.DataBean item) {
        //答案内容
        String answer1 = item.getAnswer();
        //答案时间
        long ansDate = item.getAnsDate();
        //答案头像
        String headImg = item.getHeadImg();
        //答案名字
        String name1 = item.getName();

        if (!TextUtils.isEmpty(headImg)) {
            ImageUtil.loadImageHead(this, headImg, head);
        }
        if (!TextUtils.isEmpty(name1)) {
            this.name.setText(name1);
            TextPaintUtils.textMothed(name);
        }

        String time1 = null;
        if (ansDate != 0) {
            time1 = TimeUtil.format(ansDate);
        } else {
            time1 = "";
        }
        common_date.setText(time1);
        content.setText(answer1);
    }

    private void setDataTwo(OneDetailsModel.DataBean item) {
        setDataOne(item);
        fristAskName.setText(name1);
        TextPaintUtils.textMothed(fristAskName);
        fristAskContent.setText(item.getFirstAsked());
        String time1 = null;
        if (item.getFirstAskTime() != 0) {
            time1 = TimeUtil.format(item.getFirstAskTime());
        } else {
            time1 = "";
        }
        fristAskDate.setText(time1);
    }

    private void setDataThree(OneDetailsModel.DataBean item) {
        setDataTwo(item);
        ImageUtil.loadImageHead(this, item.getHeadImg(), fristImg);
        fristName.setText(item.getName());
        TextPaintUtils.textMothed(fristName);
        fristContent.setText(item.getFirstAnswer());
        String time1 = null;
        if (item.getFirstAskAnswerTime() != 0) {
            time1 = TimeUtil.format(item.getFirstAskAnswerTime());
        } else {
            time1 = "";
        }
        fristDate.setText(time1);
    }

    private void setDataFour(OneDetailsModel.DataBean item) {
        setDataThree(item);
        secondAskName.setText(name1);
        TextPaintUtils.textMothed(secondAskName);
        secondAskContent.setText(item.getSecondAsked());
        String time1 = null;
        if (item.getSecondAskTime() != 0) {
            time1 = TimeUtil.format(item.getSecondAskTime());
        } else {
            time1 = "";
        }
        secondAskDate.setText(time1);
    }

    private void setDataFive(OneDetailsModel.DataBean item) {
        setDataFour(item);
        ImageUtil.loadImageHead(this, item.getHeadImg(), secondImg);
        secondName.setText(item.getName());
        TextPaintUtils.textMothed(secondName);
        secondContent.setText(item.getSecondAnswer());
        String time1 = null;
        if (item.getSecondAskAnswerTime() != 0) {
            time1 = TimeUtil.format(item.getSecondAskAnswerTime());
        } else {
            time1 = "";
        }
        secondDate.setText(time1);
    }

    private void initView() {
        setTitleText("一对一咨询详情");
        //我的问题信息
        question = (TextView) findViewById(R.id.question);
        mineImg = findViewById(R.id.mine_img);
        mineName = findViewById(R.id.mine_name);
        mineDate = findViewById(R.id.mine_date);
        mineMoney = findViewById(R.id.mine_money);

        image_one = (ImageView) findViewById(R.id.image_one);
        image_two = (ImageView) findViewById(R.id.image_two);
        image_three = (ImageView) findViewById(R.id.image_three);
        linearLayout = (LinearLayout) findViewById(R.id.liner);

        //没有答案时显示的文案
        noAnswer = findViewById(R.id.no_answer);

        //追问按钮
        askBtn1 = findViewById(R.id.zhui_one);
        askBtn2 = findViewById(R.id.zhui_two);

        //回答文字的横框
        answer = (RelativeLayout) findViewById(R.id.answer);
        //专家回答答案
        relativeLayout = (RelativeLayout) findViewById(R.id.relative_two);
        //我第一次追问
        relativeLayout3 = (RelativeLayout) findViewById(R.id.relative_three);
        //专家第一次回答我的追问
        relativeLayout4 = (RelativeLayout) findViewById(R.id.relative_four);
        //我第二次追问
        relativeLayout5 = (RelativeLayout) findViewById(R.id.relative_five);
        //专家第二次回答追问
        relativeLayout6 = (RelativeLayout) findViewById(R.id.relative_six);

        //专家回答我的问题信息
        head = (ImageView) findViewById(R.id.common_head);
        name = (TextView) findViewById(R.id.common_name);
        content = (TextView) findViewById(R.id.common_content);
        common_date = (TextView) findViewById(R.id.common_date);

        //我第一次追问信息
        fristAskName = findViewById(R.id.frist_ask_name);
        fristAskContent = findViewById(R.id.frist_ask_content);
        fristAskDate = findViewById(R.id.frist_ask_date);

        //专家第一次回答我追问的信息
        fristImg = findViewById(R.id.frist_answer_head);
        fristName = findViewById(R.id.frist_answer_name);
        fristContent = findViewById(R.id.frist_answer_content);
        fristDate = findViewById(R.id.frist_answer_date);

        //我第二次追问信息
        secondAskName = findViewById(R.id.second_ask_name);
        secondAskContent = findViewById(R.id.second_ask_content);
        secondAskDate = findViewById(R.id.second_ask_date);

        //专家第二次回答我第二次追问的信息
        secondImg = findViewById(R.id.second_answer_head);
        secondName = findViewById(R.id.second_answer_name);
        secondContent = findViewById(R.id.second_answer_content);
        secondDate = findViewById(R.id.second_answer_date);

        image_one.setOnClickListener(this);
        image_two.setOnClickListener(this);
        image_three.setOnClickListener(this);
        askBtn1.setOnClickListener(this);
        askBtn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_one:
                IntentManager.goBitmapShowActivity(CommonActivity.this, split, 0);
                break;
            case R.id.image_two:
                IntentManager.goBitmapShowActivity(CommonActivity.this, split, 1);
                break;
            case R.id.image_three:
                IntentManager.goBitmapShowActivity(CommonActivity.this, split, 2);
                break;
            case R.id.zhui_one:
                IntentManager.goQuestionCloselyActivity(this, uuid, price, "expert");
//                finish();
                break;
            case R.id.zhui_two:
                IntentManager.goQuestionCloselyActivity(this, uuid, price, "expert");
//                finish();
                break;
        }
    }
}
